package mini.reanimate;

import java.awt.Point;
import java.awt.event.KeyEvent;

import owg.util.euclidian.Compass;
import world.Sound;
import bogus.userInput.Input;

public class Writing extends Screen{
	private float progress;
	private int count = 0;
	
	private final Letter[] letter = new Letter[]{Letter.B, Letter.O, Letter.R, Letter.G, Letter.U, Letter.S};
	private final short[] combo = new short[]{KeyEvent.VK_B, KeyEvent.VK_O, KeyEvent.VK_R, KeyEvent.VK_G, KeyEvent.VK_U, KeyEvent.VK_S};
	
	private boolean stickVisible = false;
	private int timer;
	
	private Caption[] captions = new Caption[]{new Caption("makeenterings", "I SHOULD MAKE ENTERINGS OF THE\nNAME OF MY DECEASED BROTHER"), new Caption("borgus3", "BORGUS... BORGUS... BORGUS...")};
	private int speak = 0;
	private int speakTimer;
	
	private int keyWait;
	private double keyImg;

	public Writing() {
		super(3, "writing", "stick", "keyboard");
		sprite[1].setOrigin(Compass.NORTHWEST);
		speakTimer = 60;
		keyImg = 0;
		keyWait = 330;
	}

	@Override
	public void step() {
		if (progress < 6){
			if (!stickVisible && Input.checkFrameKey(combo[(int)progress])){
				Reanimate.me.off(0);
				Reanimate.me.off(1);
				Reanimate.me.on(2);
				Reanimate.me.on(3);
				stickVisible = true;
				new Spam("soft", (int)progress);
				Sound.playPitched("beginwrite");
				count = 0;
			}
			if (stickVisible){
				count++;
				progress += .01f;
				
				if (count >= 100){
					count = 0;
					progress = (float)Math.round(progress);
					stickVisible = false;
					Sound.playPitched("endwrite");
					enter();
					
					if (progress == 6){
						timer = 180;
						new Spam("soft", 40, 4);
						Sound.play("complete writing");
					}
				}
			}
		}
		else{
			timer--;
			if (timer <= 0){
				Reanimate.me.setLevel(Reanimate.me.LEFT);
			}
		}
		
		speakTimer--;
		if (speakTimer <= 0){
			if (progress < 3){
				captions[speak].play();
				if (speak == 0)
					speak = 1;
				else
					speak = 0;
				speakTimer = 480;
			}
		}
		
		keyWait--;
		if (keyWait == 0 && progress == 0)
			new Spam("keyboard", 5);
		
		keyImg += .03;
		if (keyImg >= 2)
			keyImg -= 2;
	}

	@Override
	public void enter() {
		Reanimate.me.BOGUS.invisible();
		Reanimate.me.off(0);
		Reanimate.me.on(1);
		Reanimate.me.off(2);
		Reanimate.me.off(3);
		Reanimate.me.BUSY = true;
	}

	@Override
	public void leave() {
		Reanimate.me.BOGUS.visible();
		Reanimate.me.written = true;
		Reanimate.me.BUSY = false;
		Reanimate.me.stick = false;
	}

	@Override
	public boolean background() {
		sprite[0].render(0, 432 + s[0].x, 243 + s[0].y, 1.01f, 1.01f, 0);
		for (int i=0; i<6; i++)
			letter[i].render(119 + s[0].x + (100 * i), 149 + s[0].y, 1, 1, progress - i);
		if (stickVisible && progress < 6){
			Point p = letter[(int)progress].getPointByPercent(progress % 1.0);
			if (p != null)
				sprite[1].render(0, p.x + s[1].x - 6 + 119 + (100 * (int)progress), p.y + s[1].y - 6 + 149);
		}
		
		if (progress == 0 && keyWait <= 0)
			sprite[2].render(keyImg, 432 + (s[2].x * 3), 243 + (s[2].y * 3));
		
		return true;
	}

	@Override
	public void foreground() {
		//fuck you
	}

	@Override
	public void bogusLand() {
		// TODO Auto-generated method stub
		
	}

}
